﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using parkMonitor.entity;
using parkMonitor.server.CoreThread;
using parkMonitor.model;
using parkMonitor.DataBase;

namespace parkMonitor.manualParking
{
    public partial class ManualParking : Form
    {
        Command storeCmd = new Command();
        IEquipments qt = EquipmentSimpleFactory.ins.FindEquipment(EquipmentName.Queue);
        DBOperation oper = new DBOperation();

        string numberRecord = "";
        string connectionStr = "SqlConnectionLocation";
        public ManualParking()
        {
            InitializeComponent();
        }

        private void store_Click(object sender, EventArgs e)
        {
            storeCmd.manual = true;
            storeCmd.commandType = 's';
            storeCmd.userID = oper.GetUserID(connectionStr,tel.Text).ToString();
            storeCmd.LicenseNum = numberPlate.Text;
            try
            {
                storeCmd.garageID = Convert.ToInt32(selectGarage.Text);
            }
            catch { MessageBox.Show("非法车库号，请重新输入"); return; }
            int garageFreeSpace = 0;
            bool isTelRegister = oper.IsTelRegister(connectionStr,tel.Text);
            if (isTelRegister == false)
            {
                MessageBox.Show("该号码未被注册，请先注册");
            }
            else if (storeCmd.garageID != 0)
            {
                garageFreeSpace = oper.getGarageFreeSpace(connectionStr,storeCmd.garageID);
                if (garageFreeSpace > 0)
                {
                    if (storeCmd.userID != "" && storeCmd.LicenseNum != numberRecord)
                    {
                        qt.SetMessage((Command)storeCmd.Clone());
                        MessageBox.Show("发送成功");
                        numberRecord = storeCmd.LicenseNum;
                    }
                    else
                    {
                        MessageBox.Show("不要重复发送指令");
                    }
                }
                else
                {
                    MessageBox.Show("该车库没有剩余车位");
                }
            }
        }

        private void fetch_Click(object sender, EventArgs e)
        {
            storeCmd.manual = true;
            storeCmd.commandType = 'f';
            storeCmd.userID = oper.GetUserID(connectionStr,tel.Text).ToString();
            storeCmd.LicenseNum = numberPlate.Text;
            storeCmd.TimeRecord = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            try
            {
                storeCmd.garageID = Convert.ToInt32(selectGarage.Text);
            }
            catch { MessageBox.Show("非法车库号，请重新输入"); return; }
            bool isTelRegister = oper.IsTelRegister(connectionStr,tel.Text);
            bool isNumberPlate = oper.IsNumberPlate(connectionStr,storeCmd.LicenseNum, storeCmd.garageID);
            if (isTelRegister == false)
            {
                MessageBox.Show("该号码未被注册，请先注册");
            }
            else if (isNumberPlate == false)
            {
                MessageBox.Show("车库中查无此车");
            }
            else
            {
                storeCmd.parkingRecordsID = oper.GetParkingRecordsID(connectionStr,storeCmd.LicenseNum);
                if (storeCmd.userID != "" && storeCmd.LicenseNum != "" && storeCmd.parkingRecordsID != 0)
                {
                    qt.SetMessage((Command)storeCmd.Clone());
                    MessageBox.Show("发送成功");
                    this.Close();
                }
                else
                {
                    MessageBox.Show("电话号码或号牌错误");
                }
            }
        }
    }
}
